package com.junyuan.fxf_pad.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.junyuan.fxf_pad.R;
import com.junyuan.fxf_pad.entity.NavMenuItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qiantao on 2016/8/26 0026.
 */
public class NavMenuAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<NavMenuItem> data;

    public NavMenuAdapter(Context context, List<NavMenuItem> data) {
        this.context = context;
        this.data = setData(data);
        inflater = LayoutInflater.from(this.context);
    }

    private List<NavMenuItem> setData(List<NavMenuItem> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        return data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        NavMenuItem bean = data.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_nav, null);
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.ll_bg = (RelativeLayout) convertView.findViewById(R.id.ll_bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.iv_icon.setImageResource(bean.getIcon());
        holder.tv_title.setText(bean.getTitle());
        if (bean.isSelected()){
            holder.tv_title.setTextColor(Color.parseColor("#2b8eb3"));
            holder.ll_bg.setBackgroundColor(Color.parseColor("#d1d1d1"));
        } else {
            holder.tv_title.setTextColor(Color.parseColor("#333333"));
            holder.ll_bg.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_title;
        ImageView iv_icon;
        RelativeLayout ll_bg;
    }
}
